package redora.rdo.client.upgrade;

import redora.rdo.configuration.rdo.gwt.client.model.RedoraConfigurationDataSource;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class UpgradePanel extends VLayout {

	boolean isInit = false;

	final String baseUrl = GWT.getModuleBaseURL()
			+ "redoraConfigurationOperations.do";

	final String baseUpgradeUrl = GWT.getModuleBaseURL() + "modelFieldsView.do";

	public UpgradePanel() {
		init();
	}

	public void init() {
		if (!isInit) {
			this.setWidth100();
			this.setHeight100();
			this.setLayoutMargin(5);
			this.setStyleName("tabSetContainer");

			final Label label = new Label();

			final ListGrid upgradeScriptList = new ListGrid();

			upgradeScriptList.setWidth(1000);
			upgradeScriptList.setHeight100();
			upgradeScriptList.setCellHeight(40);
			final RedoraConfigurationDataSource configDs = RedoraConfigurationDataSource
					.getInstance();
			upgradeScriptList.setDataSource(configDs);
			ListGridField numField = new ListGridField("id", "No.");
			ListGridField scriptField = new ListGridField("scriptName",
					"Script Name");
			scriptField.setType(ListGridFieldType.LINK);
			scriptField.addRecordClickHandler(new RecordClickHandler(){

				public void onRecordClick(RecordClickEvent event) {
					SC.say(event.getField().getName());
				}
				
			});
			ListGridField statueField = new ListGridField("status", "Status");
			ListGridField outputField = new ListGridField("Output", "Output");
			ListGridField creationDateField = new ListGridField("creationDate",
					"CreationDate");
			ListGridField updateDateField = new ListGridField("updateDate",
					"UpdateDate");

			upgradeScriptList.setFields(numField, scriptField, statueField,
					outputField, creationDateField, updateDateField);
			upgradeScriptList.fetchData();

			IButton upgrade = new IButton("upgrade");
			upgrade.addClickHandler(new ClickHandler() {
				String url = URL
						.encode(baseUpgradeUrl + "?action=upgradeTable");
				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
						url);

				public void onClick(ClickEvent arg0) {
					builder.setCallback(new RequestCallback() {
						public void onError(Request request, Throwable exception) {
							SC.warn("upgrade failed!");
						}

						public void onResponseReceived(Request request,
								Response response) {
							label.setContents(response.getText());
							upgradeScriptList.getResultSet().invalidateCache();
							upgradeScriptList.fetchData();
						}
					});
					try {
						builder.send();
					} catch (RequestException e) {
						e.printStackTrace();
					}
				}

			});

			IButton check = new IButton("check");
			check.addClickHandler(new ClickHandler() {
				String url = URL.encode(baseUpgradeUrl + "?action=check");
				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
						url);

				public void onClick(ClickEvent arg0) {
					builder.setCallback(new RequestCallback() {
						public void onError(Request request, Throwable exception) {
							SC.warn("check failed!");
						}

						public void onResponseReceived(Request request,
								Response response) {
							label.setContents(response.getText());
							upgradeScriptList.getResultSet().invalidateCache();
							upgradeScriptList.fetchData();
						}
					});
					try {
						builder.send();
					} catch (RequestException e) {
						e.printStackTrace();
					}
				}

			});
			HLayout layout = new HLayout();
			layout.setHeight("5%");
			layout.addMember(upgrade);
			layout.addMember(check);
			this.addMember(layout);
			
			upgradeScriptList.setHeight("50%");
			this.addMember(upgradeScriptList);

			//label.setHeight("50%");
			this.addMember(label);
			this.draw();
			isInit = true;
		}
	}
}
